//
//  WJS_PicScrollView.m
//  CrazyWord
//
//  Created by shange1999 on 16/2/26.
//  Copyright © 2016年 shange1999. All rights reserved.
//

#import "WJS_PicScrollView.h"

#define INDEXPATCHSECTION   6    //分区数

@interface WJS_PicScrollView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain) UIImageView      *myImageview;
@property (nonatomic,retain) UICollectionView *myCollectionview;
@property (nonatomic,retain) NSTimer          *myTime;
@property (nonatomic,strong) UIPageControl    *myPageControl;
@property (nonatomic,assign) CGRect           myframe;

@end

@implementation WJS_PicScrollView

-(instancetype)initWithFrame:(CGRect)frame{
    self =[super initWithFrame:frame];
    if (self) {
        self.myframe=frame;
        [self createView];
    }
    return self;
}

-(void)createView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize                = CGSizeMake(WIDTH, 240);
    layout.minimumInteritemSpacing = 0;//
    layout.minimumLineSpacing      = 0;
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    self.myCollectionview=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 240) collectionViewLayout:layout];
    
//    self.myCollectionview.backgroundColor=[UIColor blueColor];
    self.myCollectionview.pagingEnabled=YES;
    [self addSubview:self.myCollectionview];
    
    self.myCollectionview.delegate   = self;
    self.myCollectionview.dataSource = self;
    [self.myCollectionview registerClass:[WJS_PicScrollViewCell class] forCellWithReuseIdentifier:@"scrollCell"];
    
    self.myPageControl =[[UIPageControl alloc]initWithFrame:CGRectMake(0, self.myframe.size.height-20, WIDTH/2,20)];
    self.myPageControl.center = CGPointMake(WIDTH/2, self.myframe.size.height-20);

    [self addSubview:self.myPageControl];
    [self addTime];        //调用定时器
    if (self.picScrollArr.count==0) {
        [self.myTime invalidate];  //停止定时器
        self.myTime=nil;              //滞空定时器
    }
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    
    if (self.picScrollArr.count!=0) {
        NSLog(@"轮播图数组个数%lu",(unsigned long)self.picScrollArr.count);
         return self.picScrollArr.count;
    }else {
        NSLog(@"网络断开连接");
    return 0;
    }
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WJS_PicScrollViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"scrollCell" forIndexPath:indexPath];
    cell.PicScrollModel = self.picScrollArr[indexPath.row];    //(4)把model 传到 cell 里面model套nodel
    return cell;
}
// (3)重写setting方法后重新走 代理方法
-(void)setPicScrollArr:(NSMutableArray *)picScrollArr{
    if (_picScrollArr!=picScrollArr) {
        _picScrollArr = picScrollArr;
    }
    _myPageControl.numberOfPages=_picScrollArr.count;
    [_myCollectionview reloadData];
}
#pragma 增加定时器
-(void)addTime{
    
    self.myTime =[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changePic) userInfo:nil repeats:YES];
  
    //问题:时间不准确
    //因为 Runloop导致    当定时器加到 scrollview 或者继承scroll的控件,当滑动的时候会停止
    [[NSRunLoop currentRunLoop] addTimer:self.myTime forMode:NSDefaultRunLoopMode];
    
}
-(void)changePic{
    NSIndexPath *fixIndexpath =[self returnIndexpath];  //固定当前的分区
    NSInteger item = fixIndexpath.item+1;
    NSInteger section = fixIndexpath.section;
    if (item==self.picScrollArr.count) {
        item=0;
        section++;   //目的是消除一组滚动完成后快速回到第一个item的效果
    }
    self.myPageControl.currentPage = item;  //对应item
    NSIndexPath *nextIndexpath =[NSIndexPath indexPathForItem:item inSection:section];
    [self.myCollectionview scrollToItemAtIndexPath:nextIndexpath atScrollPosition:UICollectionViewScrollPositionLeft  animated:YES];
    
}
#pragma 设置分区的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return INDEXPATCHSECTION;
}
#pragma 固定显示的分区
-(NSIndexPath*)returnIndexpath{
     //获取当前显示的cell 的indexoath
    NSIndexPath *currentIndexpath =[[self.myCollectionview indexPathsForVisibleItems]lastObject];
   NSIndexPath * fixIndexpath =[NSIndexPath indexPathForItem:currentIndexpath.item inSection:INDEXPATCHSECTION/2];   //回滚到中间的部分.从中间的位置开始滚动
    [self.myCollectionview scrollToItemAtIndexPath:fixIndexpath atScrollPosition:UICollectionViewScrollPositionLeft  animated:NO]; //item 滚动到下一个固定的分区地点
    return fixIndexpath;
}
#pragma 手势的添加
#pragma 开始滚动移除定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{   //Drag  拖拽
    [self removeTime];  //移除
}
-(void)removeTime{
    if (self.myTime!=nil) {
        [self.myTime invalidate];  //停止定时器
        self.myTime=nil;              //滞空定时器
    }
}
#pragma 拖拽结束添加定时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTime];
    self.myPageControl.currentPage = (NSInteger)(scrollView.contentOffset.x/WIDTH)%self.picScrollArr.count;

}
#pragma collectionview的点击方法中     添加通知中心, 实现传值并且跳转页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.picScrollArr.count>0) {
        WJS_PicScrollmodel *model =self.picScrollArr[indexPath.row];
        NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:model.passID,@"passID",nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushWebView" object:nil userInfo:dic];
    }else{
        NSLog(@"无网络连接,无法点击");
    }
}


#pragma 移除
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"pushDt"];
}















/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
